package com.timebank.system.queues;

import com.timebank.common.core.constant.rabbitmq.exchange.ExchangeConstant;
import com.timebank.common.core.constant.rabbitmq.queue.QueueConstant;
import com.timebank.common.core.constant.rabbitmq.routingkey.RoutingKeyConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 智能合约 消息 队列
 * @author shiliuyinzhen
 */
@Configuration
public class ContractMQ {

    /**
     * 智能合约 消息 队列
     * @return
     */
    @Bean
    public Queue contractMessageQueue() {
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        // return new Queue("TestDirectQueue",true,true,false);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue(QueueConstant.CONTRACT_MESSAGE_QUEUE,true);
    }

    /**
     *  Direct交换机：智能合约 消息 交换机
     * @return
     */
    @Bean
    DirectExchange contractMessageDirect() {
        return new DirectExchange(ExchangeConstant.CONTRACT_MESSAGE_EXCHANGE,true,false);
    }

    /**
     * 将队列和交换机绑定, 并设置用于匹配键：volunteer.message
     *
     * @return
     */
    @Bean
    Binding bindingDirect() {
        return BindingBuilder
                .bind(contractMessageQueue()).
                to(contractMessageDirect())
                .with(RoutingKeyConstant.CONTRACT_MESSAGE_ROUTING_KEY);
    }

}
